// 这个数据，是用于：添加（修改）管理员时，设定权限
// 1、定义全部的菜单数据
export const menudata = [
    {
        id:"1",
        label:"首页",
        path:"/Admin/Myhome"
    },
    {
        id:"2",
        label:"商品",
        children:[
            {
                id:"2-1",
                label:"商品列表",
                path:"/Admin/shoppylist"
            },
            {
                id:"2-2",
                label:"商品分页",
                path:"/Admin/shoppyye"
            },
        ]
    },
    {
        id:"3",
        label:"轮播图管理",
        children:[
            {
                id:"3-1",
                label:"轮播图列表",
                path:"/Admin/List"
            },
            {
                id:"3-2",
                label:"轮播图添加",
                path:"/Admin/Add"
            }
        ]
    },
    {
        id:"4",
        label:"管理人员",
        children:[
            {
                id:"4-1",
                label:"管理人列表",
                path:"/Admin/renList"
            },
            {
                id:"4-2",
                label:"管理人添加",
                path:"/Admin/renAdd"
            }
        ]
    },
    {
        id:"5",
        label:"数据分析",
        children:[
            {
                id:"5-1",
                label:"商品与用户",
                path:"/Admin/commodity"
            },
            {
                id:"5-2",
                label:"用户数据列表",
                path:"/Admin/user"
            },
        ]
    },
    {
        id:"6",
        label:"订单",
        children:[
            {
                id:"6-1",
                label:"查看全部订单列表",
                path:"/Admin/dinglisy"
            },
            {
                id:"6-2",
                label:"修改订单列表",
                path:"/Admin/dingxiu"
            },
        ]
    },
    {
        id:"7",
        label:"简单K线图",
        children:[
            {
                id:"7-1",
                label:"K线图",
                path:"/Admin/Kxian"
            },
            {
                id:"7-2",
                label:"图表数据",
                path:"/Admin/shuju"
            },
        ]
    },
];

// 2、根据当前用户的权限，产生相应的菜单

export default function getMenus () {
    //  1）、获取该用户的权限（登录时已经保存了）
    let checkedKeys:string[] = [];// ['1','3','3-1']
    let currKeys = sessionStorage.getItem("grants");
    if (currKeys) {
        checkedKeys = JSON.parse(currKeys)
    }

    if(checkedKeys.length===0){
        checkedKeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1','5-2','6','6-1','6-2','7','7-1','7-2',];
    }

    // 2）、根据该用户的权限结合全部菜单数据，产生该用户的菜单

    interface IMenuItem{
        id:string,
        label:string,
        path?:string,
        children?:IMenuItem[]
    }

    let currMenuData:IMenuItem[] = [];//存储当前用户的菜单项

    menudata.forEach(item=>{
        if(checkedKeys.indexOf(item.id)>-1){
            //定义一个菜单项：            
            const newmenuItem = {...item};  
            // 处理子一级
            if(item.children){
                newmenuItem.children = [];
                item.children.forEach(sonItem=>{
                    if(checkedKeys.indexOf(sonItem.id)>-1){
                        newmenuItem.children?.push({...sonItem});
                    }
                })
            }
            currMenuData.push(newmenuItem);
        }
    });

    console.log("currMenuData",currMenuData);
    
    return currMenuData;
    
}